﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sparrow.Extensions
{
    public static class StringExtensions
    {
        public static bool IsNullOrEmpty(this string source)
        {
            
            if (source == null)
            {
                return true;
            }
            else
            {
                return source.Length == 0;
            }
        }

        public static T As<T>(this string source) 
        {
            if (source == null)
            {
              return default(T);
            }

            try
            {
                return (T)Convert.ChangeType(source, typeof(T));
            }
            catch
            {
                return default(T);
            }
        }

        public static string Fix(this string source, int length, char separator = '0')
        {
            var len = source.Length;
            if (len > length)
            {
                return source.Substring(0, length);
            }
            else if (len == length)
            {
                return source;
            }
            else
            {
                return source.PadRight(length, separator);
            }
        }

    }
}
